

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=dark>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/my-blog/img/fluid.png">
  <link rel="icon" href="/my-blog/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="木舟">
  <meta name="keywords" content="python Vue Test 测试开发 自动化测试">
  
    <meta name="description" content="前言目前编程方式一般分为两种  面向过程 面向对象  而 面向对象 是 高级编程语言特有的方式 也就是只要一门编程语言能够支持 面向对象 的编程写法, 那么他们就一门高级语言, 比较常见的就是 python&#x2F;java 那么这两编程方式有哪些区别? 面向过程可以理解成, 一段程序做了一系列的事情, 下面拿 制作一只好吃的鸭子 为例子  领取一只鸭子 养它长大 杀鸭子 烹饪 吃&#x2F;消化  这是一个过程,">
<meta property="og:type" content="article">
<meta property="og:title" content="python面向对象（上）">
<meta property="og:url" content="https://walker_kwu.gitee.io/my-blog/2023/03/11/python/base/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%EF%BC%88%E4%B8%8A%EF%BC%89/">
<meta property="og:site_name" content="My Blog">
<meta property="og:description" content="前言目前编程方式一般分为两种  面向过程 面向对象  而 面向对象 是 高级编程语言特有的方式 也就是只要一门编程语言能够支持 面向对象 的编程写法, 那么他们就一门高级语言, 比较常见的就是 python&#x2F;java 那么这两编程方式有哪些区别? 面向过程可以理解成, 一段程序做了一系列的事情, 下面拿 制作一只好吃的鸭子 为例子  领取一只鸭子 养它长大 杀鸭子 烹饪 吃&#x2F;消化  这是一个过程,">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-03-11T06:44:59.000Z">
<meta property="article:modified_time" content="2023-05-23T10:37:53.266Z">
<meta property="article:author" content="木舟">
<meta property="article:tag" content="测试开发">
<meta property="article:tag" content="Python基础">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>python面向对象（上） - My Blog</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/my-blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/my-blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/my-blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"walker_kwu.gitee.io","root":"/my-blog/","version":"1.9.2","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"left","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/my-blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/my-blog/js/utils.js" ></script>
  <script  src="/my-blog/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.2.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/my-blog/">
      <strong>木舟</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/my-blog/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/my-blog/img/default.png') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="python面向对象（上）"></span>
          
        </div>

        
          
  <div class="mt-3">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-author" aria-hidden="true"></i>
        木舟
      </span>
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-03-11 14:44" pubdate>
          2023年3月11日 下午
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7k 字
        
      </span>
    

    

    
    
      
        <span id="busuanzi_container_page_pv" style="display: none">
          <i class="iconfont icon-eye" aria-hidden="true"></i>
          <span id="busuanzi_value_page_pv"></span> 次
        </span>
        
      
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="padding-left: 2rem; margin-right: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">python面向对象（上）</h1>
            
              <p class="note note-info">
                
                  
                    本文最后更新于：2023年5月23日 晚上
                  
                
              </p>
            
            
              <div class="markdown-body">
                
                <h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>目前编程方式一般分为两种</p>
<ul>
<li>面向过程</li>
<li>面向对象</li>
</ul>
<p>而 <strong>面向对象</strong> 是 高级编程语言特有的方式</p>
<p>也就是只要一门编程语言能够支持 <strong>面向对象</strong> 的编程写法, 那么他们就一门高级语言, 比较常见的就是 python/java</p>
<p>那么这两编程方式有哪些区别?</p>
<p><strong>面向过程</strong>可以理解成, 一段程序做了一系列的事情, 下面拿 <strong>制作一只好吃的鸭子</strong> 为例子</p>
<ul>
<li>领取一只鸭子</li>
<li>养它长大</li>
<li>杀鸭子</li>
<li>烹饪</li>
<li>吃/消化</li>
</ul>
<p>这是一个过程, 等于把程序看成一个人, 这个人干了一系列的事情.</p>
<p>而 <strong>面向对象</strong> 就是将不同的事情交给不同的人去做</p>
<ul>
<li>领取/饲养 -&gt; A</li>
<li>宰杀烹饪 -&gt; B</li>
<li>吃消化 -&gt; C</li>
</ul>
<p>等于说我们把过程中的同类型的功能分为不同的人去做</p>
<p>那么为什么现在这么推崇 <strong>面向对象</strong>?</p>
<p>因为 <strong>面向对象</strong> 能够很好的帮我进行程序逻辑的抽离</p>
<p>可以想象的是, <strong>面向过程</strong> 如果复用后, 其中一个逻辑发生了改变, 将会产生很大的维护成本.</p>
<p>但是 <strong>面向对象</strong> 复用后, 我们只需要修改 <strong>出问题的那个人</strong> 即可, 并且我们可以根据功能的相似性来做出一些兼容.</p>
<p><strong>比如说你不仅想吃鸭子,也想吃鸡,那么就可以把原来杀鸭子的人也让他去杀鸡.</strong></p>
<p>这是一种编程思想, 经验尚浅是说了再多也不能很好的吸收, 要在大量的代码实战中才能领悟, 所以不多说废话, 我们接下来就来学习 <strong>python面向对象.</strong></p>
<h1 id="类与对象"><a href="#类与对象" class="headerlink" title="类与对象"></a>类与对象</h1><h2 id="类"><a href="#类" class="headerlink" title="类"></a>类</h2><p>面向对象是建立在如何去设计好一个 <strong>类</strong></p>
<p><strong>类</strong> 指代的是生产出对象的模板.</p>
<p>比如说: 我想要生产很多辆相同款式的宝马, 但是它们也许会有不同的配(高配/低配), 但是起码它们的外形/基础配置都是一样的.</p>
<p>那我们就可以把生产宝马的厂子成为 <strong>类</strong>.</p>
<p>这个 <strong>类</strong> 负责按照模板给你生产出 <strong>对象</strong></p>
<h2 id="对象"><a href="#对象" class="headerlink" title="对象"></a>对象</h2><p>那么 <strong>对象</strong> 是什么?</p>
<p>对象就是一个实际使用/实际存在的东西, 就比如上面的例子</p>
<p><strong>类</strong> 这个厂子, 我们只是知道它的存在, 并不能直接拿来使用, 它只是负责生产出 <strong>对象</strong>也就是车子.</p>
<p>那么生产出来的 <strong>对象</strong> 就是 卖出去的 <strong>车子</strong></p>
<p>再举个例子.</p>
<p>比如有一个 人 <strong>类</strong></p>
<p>这个 <strong>类</strong> 可以根据你传入的 <strong>参数</strong> 来生产出一个人</p>
<p>参数可以有: 姓名/年龄/性别 </p>
<h2 id="类与对象的关系"><a href="#类与对象的关系" class="headerlink" title="类与对象的关系"></a>类与对象的关系</h2><p>所以类和对象的关系, 本质上就是 <strong>生产引用</strong> 关系</p>
<p><strong>类负责生产对象</strong></p>
<p><strong>对象的基础参数由类提供</strong></p>
<h1 id="类与对象的定义"><a href="#类与对象的定义" class="headerlink" title="类与对象的定义"></a>类与对象的定义</h1><h2 id="定义一个类"><a href="#定义一个类" class="headerlink" title="定义一个类"></a>定义一个类</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-comment"># 定义一个类</span><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">类名</span>:<br>    ...<br><br><span class="hljs-keyword">class</span> <span class="hljs-title class_">类名</span>():<br>    ...<br></code></pre></td></tr></table></figure>

<p>类名的命名规则一般按照 <strong>大驼峰命名法</strong>,  每个单词首字母大写</p>
<p>以上两种方式都可以定义一个类, 那么 <strong>括号</strong> 的作用是为什么?</p>
<p>这个括号大有用处, <strong>类的三大特性</strong>有关, 我们后续再说**.</p>
<h2 id="定义一个对象"><a href="#定义一个对象" class="headerlink" title="定义一个对象"></a>定义一个对象</h2><p><strong>对象和类是生产引用关系</strong></p>
<p>所以对象要通过类来生产</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Human</span>():<br>    ...<br><br>jack = Human()<br>rose = Human()<br></code></pre></td></tr></table></figure>

<p>生产出来的对象拥有类的一切属性和方法</p>
<h1 id="类属性"><a href="#类属性" class="headerlink" title="类属性"></a>类属性</h1><p>之前我们说过, 类可以根据传入的参数来生产出对象</p>
<p>所以自然也就有 <strong>属性</strong> 来进行区分到底能够生产出来怎么样的对象.</p>
<p>类属性分为两种:</p>
<ul>
<li>公共属性, 也叫 静态属性, 每个通过类生产出来的对象的静态属性都是一样的</li>
<li>实例属性, 每个通过类生产出来的对象的实例属性都有可能是不同的</li>
<li>私有属性, 这个属性只能在 <strong>类方法</strong> 中使用</li>
</ul>
<h2 id="公共属性"><a href="#公共属性" class="headerlink" title="公共属性"></a>公共属性</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span>():<br>    brand = <span class="hljs-string">&quot;BMW&quot;</span><br><br>car1 = Car()<br>car2 = Car()<br><br><span class="hljs-built_in">print</span>(car1.brand)<br><span class="hljs-built_in">print</span>(car2.brand)<br></code></pre></td></tr></table></figure>

<p>一个Car类, 负责生产车子, 那么我们可以把它品牌当作一个 <strong>公共属性</strong></p>
<p>每个从这个类弄出来的车子,都是一个牌子的.</p>
<p>这个就是 <strong>公共属性</strong>, 每个对象都一样.</p>
<p>但是如果哪天这个 <strong>车厂子</strong> 换牌子了, 我们应该直接修改这个类的 <strong>公共属性</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span>():<br>    brand = <span class="hljs-string">&quot;BMW&quot;</span><br><br>car1 = Car()<br>car2 = Car()<br><br><span class="hljs-built_in">print</span>(car1.brand) <span class="hljs-comment"># BMW</span><br><span class="hljs-built_in">print</span>(car2.brand) <span class="hljs-comment"># BMW</span><br><br>car1.brand = <span class="hljs-string">&quot;Benz&quot;</span><br><span class="hljs-built_in">print</span>(car1.brand) <span class="hljs-comment"># Benz</span><br><span class="hljs-built_in">print</span>(Car.brand) <span class="hljs-comment"># BMW</span><br><br>Car.brand = <span class="hljs-string">&quot;Quattro&quot;</span><br><br>car3 = Car()<br><br><span class="hljs-built_in">print</span>(Car.brand)  <span class="hljs-comment"># Quattro</span><br><span class="hljs-built_in">print</span>(car3.brand)  <span class="hljs-comment"># Quattro</span><br></code></pre></td></tr></table></figure>

<h2 id="实例属性"><a href="#实例属性" class="headerlink" title="实例属性"></a>实例属性</h2><p>实例属性可以通过以下方式来创建</p>
<ul>
<li>在定义类的具体某个方法(函数)中直接使用 self.xxx = xxx 这种方式等我们学习了 <strong>类方法</strong> 后再说明</li>
<li>直接对象.xxx = xxx</li>
</ul>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span>():<br>    brand = <span class="hljs-string">&quot;BMW&quot;</span><br>    height = <span class="hljs-number">1400</span><br>    width = <span class="hljs-number">4000</span><br><br><br>car1 = Car()<br>car2 = Car()<br><br><br>car1.color = <span class="hljs-string">&quot;blue&quot;</span><br>car2.color = <span class="hljs-string">&quot;red&quot;</span><br><br><span class="hljs-built_in">print</span>(car1.color)  <span class="hljs-comment"># blue</span><br><span class="hljs-built_in">print</span>(car2.color)  <span class="hljs-comment"># red</span><br></code></pre></td></tr></table></figure>

<p>我们将之前的 <strong>车厂子</strong> 增加了几个 公共属性, 车高/车宽</p>
<p>然后生产出来的车子, 每台车的颜色都是定制的, 所以颜色都有可能不同.</p>
<p>所以我们可以通过 <strong>实例属性</strong> 来定义不同的车子的颜色.</p>
<p>值得注意的是, <strong>公共属性</strong> 在实例化出来一个对象之后, 都会变成这个对象的 <strong>实例属性</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span>():<br>    brand = <span class="hljs-string">&quot;BMW&quot;</span><br>    height = <span class="hljs-number">1400</span><br>    width = <span class="hljs-number">4000</span><br><br><br>car1 = Car()<br>car2 = Car()<br><br>car1.height = <span class="hljs-number">1000</span><br><span class="hljs-built_in">print</span>(car1.height)  <span class="hljs-comment"># 1000</span><br><span class="hljs-built_in">print</span>(car2.height)  <span class="hljs-comment"># 1400</span><br></code></pre></td></tr></table></figure>

<h2 id="私有属性"><a href="#私有属性" class="headerlink" title="私有属性"></a>私有属性</h2><p>私有属性只能在 <strong>类定义的方法中使用</strong></p>
<p>私有属性就是 <strong>私有化</strong>, 外部不可见, 想要使用只能通过类特定的方法来获取.</p>
<p>其目的就是保护某些敏感数据.</p>
<p>假设这个 <strong>私有属性</strong> 是一个极其敏感的密码字符串</p>
<p>你必须要通过这个方法的身份校验才可以获取到.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span>():<br>    __password = <span class="hljs-string">&quot;123456&quot;</span>  <span class="hljs-comment"># 双下划线开头</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">getPassword</span>(<span class="hljs-params">self, name</span>):<br>        <span class="hljs-keyword">if</span> name == <span class="hljs-string">&quot;muzhou&quot;</span>:<br>            <span class="hljs-keyword">return</span> self.__password<br>        <span class="hljs-keyword">else</span>:<br>            <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;身份校验失败, 无法获取&quot;</span><br><br><br>password = Password()<br><br><span class="hljs-built_in">print</span>(password.getPassword(<span class="hljs-string">&quot;mz&quot;</span>))<br><span class="hljs-built_in">print</span>(password.getPassword(<span class="hljs-string">&quot;muzhou&quot;</span>))<br><span class="hljs-built_in">print</span>(password.__password)  <span class="hljs-comment"># 无法直接获取, 会报错</span><br></code></pre></td></tr></table></figure>

<p>其中涉及到的 <strong>类方法</strong> 会在下面进行直接, 这里只需要理解 <strong>私有属性</strong> 的特性以及应用情况即可.</p>
<h1 id="类方法"><a href="#类方法" class="headerlink" title="类方法"></a>类方法</h1><p>类方法就是这个类所具备的功能，比如跑步/吃饭</p>
<p>类方法按照用法/特性可以区分为几种:</p>
<ul>
<li>实例方法, 对象特有的方法, 和 <strong>实例属性</strong> 类似, 使用较为频繁</li>
<li>类方法, 公共方法, 类和对象都可以调用, 和 <strong>公共属性</strong> 类似</li>
<li>静态方法, 类和对现象都可以调用, 但是该方法中不能调用类或对象的属性</li>
<li>私有方法, 和 <strong>私有属性</strong>类似</li>
<li>魔法方法, 用处较多, 种类较多, 下面会介绍几种常用的.</li>
</ul>
<h2 id="实例方法"><a href="#实例方法" class="headerlink" title="实例方法"></a>实例方法</h2><p>最少接收一个参数 <strong>self</strong></p>
<p>这个self指代的就是 <strong>对象本身</strong></p>
<p>所以实例方法只能被 <strong>对象所调用</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Human</span>():  <span class="hljs-comment"># 有手有脚的人啊</span><br>    hands = <span class="hljs-literal">True</span><br>    feet = <span class="hljs-literal">True</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">work</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-keyword">if</span> self.hands <span class="hljs-keyword">and</span> self.feet:<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">&#123;self.name&#125;</span>有手有脚就该干活&quot;</span>)<br>        <span class="hljs-keyword">else</span>:<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;不歧视, 多照顾<span class="hljs-subst">&#123;self.name&#125;</span>&quot;</span>)<br><br><br>jack = Human()<br>jack.name = <span class="hljs-string">&quot;jack&quot;</span><br>rose = Human()<br>rose.name = <span class="hljs-string">&quot;rose&quot;</span><br><br>jack.work()<br><br>rose.work()<br><br><span class="hljs-comment"># 发生意外</span><br>jack.feet = <span class="hljs-literal">False</span><br><br>jack.work()<br><br>Human.work()  <span class="hljs-comment"># 报错, 你都不是个人还想干活</span><br></code></pre></td></tr></table></figure>

<p>验证上述所说 <strong>实例方法只能被对象调用, 因为对象调用时, 会自动将 参数self 赋值成本身</strong></p>
<p>所以实例方法才能通过self获取到对象本身的 <strong>实例属性</strong></p>
<h2 id="类方法-1"><a href="#类方法-1" class="headerlink" title="类方法"></a>类方法</h2><p>类方法和 <strong>实例方法</strong> 不同的是 ,他需要由 <strong>内置装饰器@classmethod</strong>装饰</p>
<p>并且接收的参数由 <strong>self</strong> 变成 <strong>cls</strong></p>
<p>所以它只能调用类的方法/属性, 对象的属性/方法它无法调用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Car</span>():<br>    brand = <span class="hljs-string">&quot;BWM&quot;</span><br><br><span class="hljs-meta">    @classmethod</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">classInfo</span>(<span class="hljs-params">cls</span>):<br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;车子都是<span class="hljs-subst">&#123;cls.brand&#125;</span>厂子生产的&quot;</span>)<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">selfInfo</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-comment"># Car.classInfo()  这么调用也可以</span><br>        self.classInfo() <span class="hljs-comment"># 方法内可以调用类中定义的方法</span><br>        <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;车子的颜色是<span class="hljs-subst">&#123;self.color&#125;</span>&quot;</span>)<br><br><br>car1 = Car()<br>car2 = Car()<br><br>car1.color = <span class="hljs-string">&quot;blue&quot;</span><br>car2.color = <span class="hljs-string">&quot;red&quot;</span><br><br><span class="hljs-comment"># 车子都是BWM厂子生产的</span><br><span class="hljs-comment"># 车子的颜色是blue</span><br>car1.selfInfo()<br><br><span class="hljs-comment"># 车子都是BWM厂子生产的</span><br><span class="hljs-comment"># 车子的颜色是red</span><br>car2.selfInfo()<br><br>Car.brand = <span class="hljs-string">&quot;Benz&quot;</span><br><br><span class="hljs-comment"># 车子都是Benz厂子生产的</span><br><span class="hljs-comment"># 车子的颜色是blue</span><br>car1.selfInfo()<br><br><span class="hljs-comment"># 车子都是Benz厂子生产的</span><br><span class="hljs-comment"># 车子的颜色是red</span><br>car2.selfInfo()<br><br><br>car1.brand = <span class="hljs-string">&quot;Quattro&quot;</span><br><br><span class="hljs-comment"># 车子都是Benz厂子生产的</span><br><span class="hljs-comment"># 车子的颜色是blue</span><br>car1.selfInfo()<br><br><span class="hljs-comment"># 车子都是Benz厂子生产的</span><br><span class="hljs-comment"># 车子的颜色是red</span><br>car2.selfInfo()<br></code></pre></td></tr></table></figure>

<p>通过结果我们可以知道, 类方法只会调用 类本身的属性/方法</p>
<h2 id="静态方法"><a href="#静态方法" class="headerlink" title="静态方法"></a>静态方法</h2><p>静态方法就是一个普通的函数, 可以被类/对象调用</p>
<p>它不接收 <strong>cls/self</strong> 等参数, 所以它无法调用 类/对象的属性/方法</p>
<p>就是一个普通的函数</p>
<p>你可以将一个类当作一种工具集, 里面的静态方法就是不同的工具.</p>
<p>静态方法需要使用 <strong>内置装饰器@staticmethod</strong>装饰</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Tools</span>():<br><br><span class="hljs-meta">    @staticmethod</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">getPath</span>():  <span class="hljs-comment"># 获取特定文件的路径</span><br>        ...<br><br><span class="hljs-meta">    @staticmethod</span><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">getTime</span>():  <span class="hljs-comment"># 获取时间</span><br>        ...<br></code></pre></td></tr></table></figure>

<h2 id="私有方法"><a href="#私有方法" class="headerlink" title="私有方法"></a>私有方法</h2><p>定义时需要在前面加上 <strong>双下划线</strong></p>
<p>和 <strong>私有属性</strong> 一样</p>
<p>只能在 <strong>类定义的方法中使用</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Password</span>():<br>    __password = <span class="hljs-string">&quot;123456&quot;</span>  <span class="hljs-comment"># 双下划线开头</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__check</span>(<span class="hljs-params">self, name</span>):<br>        <span class="hljs-keyword">return</span> name == <span class="hljs-string">&quot;muzhou&quot;</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">getPassword</span>(<span class="hljs-params">self, name</span>):<br>        <span class="hljs-comment"># 这里用到了 三元表达式, 忘了的同学复习一下哦</span><br>        <span class="hljs-keyword">return</span> self.__password <span class="hljs-keyword">if</span> self.__check(name) <span class="hljs-keyword">else</span> <span class="hljs-string">&quot;身份校验失败, 无法获取&quot;</span><br><br><br>password = Password()<br><br><span class="hljs-built_in">print</span>(password.getPassword(<span class="hljs-string">&quot;mz&quot;</span>))<br><span class="hljs-built_in">print</span>(password.getPassword(<span class="hljs-string">&quot;muzhou&quot;</span>))<br></code></pre></td></tr></table></figure>

<h2 id="魔法方法"><a href="#魔法方法" class="headerlink" title="魔法方法"></a>魔法方法</h2><p>魔法方法是python面向对象的 <strong>高阶知识</strong></p>
<p>魔法方法都是 <strong>双下划线开始双下划线结束</strong></p>
<p>可以说, python的面向对象的构成都是由这些 <strong>魔法方法</strong> 所堆砌而成.</p>
<p>所以说, 每个生产出来的对象都会具备所有的 <strong>魔法方法</strong></p>
<p>你可以自定义去重写这些魔法方法, 也可以就这么滴. 一般初学者只会用到其中的 <code>__init__</code></p>
<p>其他魔法方法就先由他们去吧, 等我们逐步掌握好python后, 再来研究他们.</p>
<p>下面就介绍一下 <code>__init__</code></p>
<p>这个魔法方法, 主要是用来 <strong>初始化对象属性</strong></p>
<p>之前我们的例子中, 有车子有人类, 但是我们都是在创建它们之后才去修改它们的 <strong>实例属性</strong></p>
<p>这种方式通常来说是不会看到的, 因为我们可以使用 <code>__init__</code>来帮我们做这件事</p>
<p><code>__init__</code>属于 <strong>实例方法</strong> 所以会接收一个参数 <code>self</code></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Human</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self</span>):<br>        self.name = <span class="hljs-string">&quot;jack&quot;</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">info</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(self.name)<br><br>jack = Human()<br>jack.info()<br></code></pre></td></tr></table></figure>

<p>运行后我们发现, jack这个对象已经有了自己的名字</p>
<p>但是同样的, 我们发现了其他缺点, 就是这个名字还是写死了, 很不方便.</p>
<p>所以 <code>__init__</code>也可以接收更多的参数, 用来帮助我们初始化对象属性</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Human</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, name, sex, age</span>):<br>        self.name = name<br>        self.sex = sex<br>        self.age = age<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">info</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(self.name)<br>        <span class="hljs-built_in">print</span>(self.sex)<br>        <span class="hljs-built_in">print</span>(self.age)<br><br>jack = Human(<span class="hljs-string">&#x27;jack&#x27;</span>, <span class="hljs-string">&#x27;male&#x27;</span>, <span class="hljs-number">18</span>)  <span class="hljs-comment"># __init__的参数必须要在实例化对象的时候传入类中</span><br>rose = Human(<span class="hljs-string">&#x27;rose&#x27;</span>, <span class="hljs-string">&#x27;female&#x27;</span>, <span class="hljs-number">19</span>)<br><br>jack.info()<br>rose.info()<br></code></pre></td></tr></table></figure>

<p>这样我们就可以通过一个类来定制生产不同属性的对象.</p>
<p>也许会有人有疑问, 那我不使用 <code>__init__</code>, 单独定义个一个方法来实例化这些属性可以不?</p>
<p>可以:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Human</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">initInfo</span>(<span class="hljs-params">self, name, sex, age</span>):<br>        self.name = name<br>        self.sex = sex<br>        self.age = age<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">info</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(self.name)<br>        <span class="hljs-built_in">print</span>(self.sex)<br>        <span class="hljs-built_in">print</span>(self.age)<br><br>jack = Human()<br>jack.initInfo(<span class="hljs-string">&#x27;jack&#x27;</span>, <span class="hljs-string">&#x27;male&#x27;</span>, <span class="hljs-number">18</span>)<br>jack.info()<br></code></pre></td></tr></table></figure>

<p>这样确实可以达到一样的效果, 但是 <strong>没有必要</strong></p>
<p>因为会多写一行代码, 并且这是<strong>不符合规范</strong>的事情.</p>
<p><code>__init__</code>执行时机就是在<strong>创建对象的时候就会执行</strong>, 所以单独定义一个初始化方法<strong>完全是多余的</strong>.</p>
<h3 id="再说一个和后面内容有关的魔法方法"><a href="#再说一个和后面内容有关的魔法方法" class="headerlink" title="再说一个和后面内容有关的魔法方法"></a>再说一个和后面内容有关的魔法方法</h3><p><code>__str__</code></p>
<p>不知道会不会有同学会试着打印一个对象, 如果有的话, 你会得到一个看不太懂的东西.</p>
<p>那么我们可以不可以定义打印对象是的输出, 答案是可以的.</p>
<p>我们回想一下 <code>print</code> 函数, 这个函数执行原理, 就是调用 <strong>打印对象的__str__</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">Human</span>():<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, name, sex, age</span>):<br>        self.name = name<br>        self.sex = sex<br>        self.age = age<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">info</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-built_in">print</span>(self.name)<br>        <span class="hljs-built_in">print</span>(self.sex)<br>        <span class="hljs-built_in">print</span>(self.age)<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__str__</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">f&quot;我叫<span class="hljs-subst">&#123;self.name&#125;</span>, 我的性别是<span class="hljs-subst">&#123;self.sex&#125;</span>, 我今年<span class="hljs-subst">&#123;self.age&#125;</span>岁&quot;</span><br><br>jack = Human(<span class="hljs-string">&#x27;jack&#x27;</span>, <span class="hljs-string">&#x27;male&#x27;</span>, <span class="hljs-number">18</span>)<br><span class="hljs-built_in">print</span>(jack)  <span class="hljs-comment"># 我叫jack, 我的性别是male, 我今年18岁</span><br></code></pre></td></tr></table></figure>

<p>所以像我们之前打印的 <strong>字符串/列表/字典</strong> 等等等, 其实它们本身就是一个对象</p>
<p>比如说列表的时候, 其实就是创建列表对象, 这是python内部的原理了, 这里就不过多赘述了.</p>
<p>所以我们也可以自己模拟做出来一个<strong>定制列表类</strong></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs python"><span class="hljs-keyword">class</span> <span class="hljs-title class_">CustomList</span>():<br><br>    __lst = []<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, maxLength</span>):<br>        self.maxLength = <span class="hljs-number">10</span><br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">add</span>(<span class="hljs-params">self, el</span>): <br>        <span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(self.__lst) &lt; <span class="hljs-number">10</span>:<br>            self.__lst.append(el)<br>        <span class="hljs-keyword">else</span>:<br>            <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;我最多只能存放<span class="hljs-subst">&#123;self.maxLength&#125;</span>个元素&quot;</span>)<br>        <br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">getLst</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-keyword">return</span> self.__lst<br><br>    <span class="hljs-keyword">def</span> <span class="hljs-title function_">__str__</span>(<span class="hljs-params">self</span>):<br>        <span class="hljs-keyword">return</span> <span class="hljs-built_in">str</span>(self.__lst)<br><br><br>lst = CustomList(<span class="hljs-number">10</span>)<br><br><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-number">10</span>):<br>    lst.add(i)<br><br><span class="hljs-built_in">print</span>(lst)  <span class="hljs-comment"># [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]</span><br> <br>lst.add(<span class="hljs-string">&#x27;&#x27;</span>) <span class="hljs-comment"># 我最多只能存放10个元素</span><br></code></pre></td></tr></table></figure>

<p>所以我们可以面向对象的方式, 定义一个只能存放某些数量的列表.</p>

                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/my-blog/categories/python/" class="category-chain-item">python</a>
  
  
    <span>></span>
    
  <a href="/my-blog/categories/python/Python%E5%9F%BA%E7%A1%80/" class="category-chain-item">Python基础</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/my-blog/tags/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/">#测试开发</a>
      
        <a href="/my-blog/tags/Python%E5%9F%BA%E7%A1%80/">#Python基础</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>python面向对象（上）</div>
      <div>https://walker_kwu.gitee.io/my-blog/2023/03/11/python/base/python面向对象（上）/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>木舟</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年3月11日</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>更新于</div>
          <div>2023年5月23日</div>
        </div>
      
      <div class="license-meta-item">
        <div>许可协议</div>
        <div>
          
            
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="BY - 署名">
                <i class="iconfont icon-by"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="NC - 非商业性使用">
                <i class="iconfont icon-nc"></i>
              </span>
              </a>
            
              <a target="_blank" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
              <span class="hint--top hint--rounded" aria-label="SA - 相同方式共享">
                <i class="iconfont icon-sa"></i>
              </span>
              </a>
            
          
        </div>
      </div>
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/my-blog/2023/03/11/python/base/python%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%EF%BC%88%E4%B8%8B%EF%BC%89/" title="python面向对象（下）">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">python面向对象（下）</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/my-blog/2023/03/05/python/base/python%E5%87%BD%E6%95%B0/" title="python函数">
                        <span class="hidden-mobile">python函数</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <span>By 木舟</span> <i class="iconfont icon-love"></i> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/my-blog/js/events.js" ></script>
<script  src="/my-blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/my-blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      headingSelector : CONFIG.toc.headingSelector || 'h1,h2,h3,h4,h5,h6',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      collapseDepth   : CONFIG.toc.collapseDepth || 0,
      scrollSmooth    : true,
      headingsOffset  : -boardTop
    });
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.10/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/my-blog/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/my-blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
